define(function(require, exports, module) {

	function onload(api) {
		return {
			name: 'CascadeSelector',
			props: {
				width: {
					type: String,
					default: '100%',
					required: false
				},
				// 每行几列
				colCountOfRow: {
					type: Number,
					default: 2,
					required: false
				},
				labelWidth: {
					type: String,
					default: '100px',
					required: false
				},
				value: {
					type: Array,
					required: true
				},
				/**
			  * 回显的API
				 */
				showApi: {
					type: Function,
					required: true
				},
				/**
				 * 选择参数
			  *  label: String, 标签名
				 *  value: 选择的值
				 *  optionsSource: 请求选项的函数
				 *  detailSource: 请求详情的函数
				 *  optionsSourceResultKey: 请求相应的数据键
				 *  optionParams: 选项函数默认参数
				 *  optionParamsValueKey: 值查询的字段名
				 *  optionLabelKey: 选项标签的字段名, 模糊查询的字段名
				 *  optionValueKey: 选项值的字段名
				 */
				params: {
					type: Array,
					required: true
				}
			},
			data() {
				return {
					currentValue: this.value,
					currentLabel: []
				}
			},
			computed: {
				cascadeSelectorStyle() {
					return {
						width: this.width,
						'--content': 'calc(100% - ' + this.labelWidth + ')'
					}
				}
			},
			created() {
				this.resetSelectors(this.value)
			},
			methods: {
				// 获得当前选中的标签数组
				getSelectedLabels() {
					return this.currentLabel
				},
				/**
				 * 重置选择器
				 * @param newValue 要重置的值
				 */
				resetSelectors(newValue) {
					this.currentValue = newValue
					if (this.params.length > 0) {
						const lastParamIndex = this.params.length - 1
						const lastValue = this.currentValue[lastParamIndex]
						if (lastValue) {
							// 有值代表回显
							// const currentValue = []
							// const currentLabel = []
							// const setOptions = (paramIndex, selectedValue) => {
							//     if (paramIndex >= 0) {
							//         const param = this.params[paramIndex]
							//         param.detailSource(selectedValue).then(res => {
							//             const pid = res.data[param.optionParamsValueKey]
							//             param.optionParams[param.optionParamsValueKey] = pid
							//             param.optionsSource(param.optionParams).then(res => {
							//                 param.options = res[param.optionsSourceResultKey]
							//                 currentValue[paramIndex] = selectedValue
							//                 currentLabel[paramIndex] = param.options.find(option => option[param.optionValueKey] === selectedValue)[param.optionLabelKey]
							//                 paramIndex--
							//                 setOptions(paramIndex, pid)
							//             })
							//         })
							//     } else {
							//         this.currentValue = currentValue
							//         this.currentLabel = currentLabel
							//     }
							// }
							// setOptions(lastParamIndex, lastValue)
							$.ajax({
								url: alluse.bigcenter + api.showApi + this.params.length + "/" + lastValue,
								type: 'GET',
								dataType: 'json',
								contentType: 'application/json',
								data: this.queryParams,
								success: (response) => {
									const allOptions = response.data.allOptions
									for (let index = 0; index < allOptions.length; index++) {
										this.params[index].options = allOptions[index]
										this.currentValue[index] = response.data.values[index]
										this.currentLabel[index] = response.data.labels[index]
									}
								}
							});
						} else {
							for (let i = 0; i < this.params.length; i++) {
								this.params[i].options = []
								this.currentValue[i] = null
							}
							const param = this.params[0]
							$.ajax({
								url: alluse.bigcenter + api.param.optionsSource,
								type: 'GET',
								dataType: 'json',
								contentType: 'application/json',
								data: param.optionParams,
								success: (response) => {
									param.options = response[param.optionsSourceResultKey]
								}
							});
						}
					}
				},
				changeSelected(selectedValue, selectIndex) {
					// 下一个选择器的下标
					const nextSelectIndex = selectIndex + 1
					if (selectedValue) {
						// 如果还有下一个
						if (this.params.length !== nextSelectIndex) {
							const param = this.params[nextSelectIndex]
							param.optionParams[param.optionParamsValueKey] = selectedValue
							$.ajax({
								url: alluse.bigcenter + api.param.optionsSource,
								type: 'GET',
								dataType: 'json',
								contentType: 'application/json',
								data: param.optionParams,
								success: (response) => {
									this.currentValue[nextSelectIndex] = null
									param.options = res[param.optionsSourceResultKey]
								}
							});
						}
						const param = this.params[selectIndex]
						this.currentLabel[selectIndex] = param.options.find(option => option[param
							.optionValueKey] === selectedValue)[param.optionLabelKey]
						this.currentLabel[nextSelectIndex] = null
					} else {
						this.currentValue[nextSelectIndex] = null
						this.currentLabel[selectIndex] = null
						this.currentLabel[nextSelectIndex] = null
						this.params[nextSelectIndex] = []
					}
					// 重新选择, 清空后面的数据
					for (let i = nextSelectIndex + 1; i < this.params.length; i++) {
						this.params[i].options = []
						this.currentValue[i] = null
						this.currentLabel[i] = null
					}
				}
			}
		}
	}

	module.exports = {
		onload
	};
});
